<template>
  <div class="app-container">
    <!-- <div class="inputBox">
      <el-input v-model="search.orderNumbers" class="input" placeholder="请输入订单号查询" />
      <el-select v-model="search.vipId" class="teacherStatus" placeholder="请选择会员等级">
        <el-option label="泰山美妈" value="1" />
        <el-option label="泰山美妈团" value="2" />
        <el-option label="泰山美妈社" value="3" />
        <el-option label="泰山美妈会" value="4" />
      </el-select>
      <el-input v-model="search.name" class="input" placeholder="请输入购买人查询" />
      <el-input v-model="search.phone" class="input" placeholder="请输购买人手机号查询" />
      <el-date-picker
        v-model="search.startTime"
        class="startDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择开始日期"/>
      <el-date-picker
        v-model="search.endTime"
        class="endDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择结束日期"/>
      <el-button type="primary" class="ml10" @click="toSearch">查询</el-button>
      <el-button type="primary" @click="toReset">重置</el-button>
    </div> -->
    <!-- <BankView ref="bankDialog" :visible="visible" :dialog-type="'order'" @isDialog="isDialog" />
    <Dialog ref="dialog" :showdialog="showDialog" :bankname="bankName" :tid="tid" :coursesection="courseSection" :orderid="orderId" :bankid="bankId" :type="type" @isDialog="isDialog" />
    <div class="inputBox">
      <el-input v-model="search.orderNum" class="input" placeholder="请输入订单编号查询" width="95"/>
      <el-input v-model="search.title" class="input" placeholder="请输入课程标题查询" />
      <el-input v-model="search.buyer" class="input" placeholder="请输入购买人人查询" />
      <el-input v-model="search.buyPhone" class="input" placeholder="请输入购买人手机号查询" />
      <el-date-picker v-model="search.startTime" class="startDate" type="date" placeholder="选择开始日期"/>
      <el-date-picker v-model="search.endTime" class="endDate" type="date" placeholder="选择结束日期"/>
      <el-select v-model="search.status" class="teacherStatus" placeholder="请选订单状态">
        <el-option label="已付款" value="100" />
        <el-option label="已提现" value="500" />
        <el-option label="退款中" value="650" />
        <el-option label="已退款" value="700" />
        <el-option label="已完成" value="800" />
      </el-select>
      <el-button type="primary" class="ml10" @click="toSearch()">查询</el-button>
      <el-button type="primary" @click="toReset()">重置</el-button>
    </div> -->
    <el-button v-if="setPermissionBtn('/companyAccount/add', addBtn)" type="primary" class="addBtn" @click="toaddPage()">{{ addBtn.btnName }}</el-button><br><br>

    <h3 class="title">对公账号管理</h3>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中请稍后"
      border
      fit
      highlight-current-row>
      <template slot="empty">
        <div class="noData">暂无数据</div>
      </template>
      <el-table-column label="公司名称">
        <template slot-scope="scope">
          {{ scope.row.companyName }}
        </template>
      </el-table-column>
      <el-table-column label="银行账号" width="130">
        <template slot-scope="scope">
          {{ scope.row.bankCard | hideBankCard }}
        </template>
      </el-table-column>
      <el-table-column label="开户行名称">
        <template slot-scope="scope">
          {{ scope.row.bankName }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.enableStatus === 0">停用</span>
          <span v-else-if="scope.row.enableStatus === 1">启用</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="290">
        <template slot-scope="scope">
          <el-button
            v-if="setPermissionBtn('/companyAccount/edit', scope.row)"
            :disabled="scope.row.enableStatus === 1"
            value="编辑"
            size="mini"
            type="primary"
            @click="edit(scope.row)">{{ scope.row.btnName }}</el-button>
          <el-button
            v-if="setPermissionBtn('/companyAccount/enableStatus', scope.row)"
            :type="scope.row.enableStatus == 0?'success':'danger'"
            size="mini"
            value="启用/停用"
            @click="update(scope.row.id, scope.row.enableStatus)"
            v-html="splitName(scope.row.btnName, scope.row.enableStatus)"/>
          <el-button
            v-if="setPermissionBtn('/companyAccount/del', scope.row)"
            :disabled="scope.row.enableStatus === 1"
            value="删除"
            size="mini"
            type="danger"
            @click="del(scope.row.id)">{{ scope.row.btnName }}</el-button>
            <!-- <el-button
            size="mini"
            type="success"
          @click="refund(scope.row.oid, scope.row.tid, scope.row.orderCommission)">查看详情</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- <remark :remark-list="remarkList" :remark-visible.sync="remarkVisible" :remark-index="remarkIndex" /> -->
    <!-- <remarkPropt
      ref="remarke"
      :remark-propt-list="remarkProptList"
      :remark-propt-visible.sync="remarkProptVisible"
      :remark-propt-index="remarkProptIndex"
      :remark-type="-1"
      @remarkText="remarkText" /> -->
    <detail :detialvisible.sync="dialogTableVisible" :dialogdata.sync="list" :myindex.sync="detailIndex" :detail-mode="'address'" />
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
// import BankView from '@/components/Dialog/BankView'
import detail from '@/components/detail'
import Dialog from '@/components/Dialog'
import { Message } from 'element-ui'
import { getOrderList, selectCard, addRemark, getRemarkPage } from '@/api/order'
import { getLocalUid, setLocalUid, getLocalId, setLocalId } from '@/utils/uid'
import { getPermission } from '@/api/permission'
import { getLocalUserName, setLocalUserName } from '@/utils/uid'
import remark from '@/components/remark'
import remarkPropt from '@/components/remark/remarkPrompt'
import { getCompanyAccountList, addCompanyAccount, enableStatus, delCompanyAccount } from '@/api/companyAccount'
import { provinceAndCityData, regionData, CodeToText, TextToCode } from 'element-china-area-data'

export default {
  components: { Pagination, Dialog, detail, remarkPropt },
  filters: {
    // statusFilter(status) {
    //   const statusMap = {
    //     published: 'success',
    //     draft: 'gray',
    //     deleted: 'danger'
    //   }
    //   return statusMap[status]
    // },
    // 等于之前的level
    formatvipId(vipId) {
      const vipIdMap = {
        1: '泰山美妈',
        2: '泰山美妈团',
        3: '泰山美妈社',
        4: '泰山美妈会'
      }
      return vipIdMap[vipId]
    },

    nullStr(str) {
      if (str === '' || str === null) {
        return '-'
      } else {
        return str
      }
    },
    provinceCodeStr(provinceCode) {
      return CodeToText[[provinceCode]]
    },
    cityCodeStr(cityCode) {
      if (cityCode === '120100' || cityCode === '110100') {
        return ''
      } else {
        return CodeToText[[cityCode]]
      }
    },
    countyCodeStr(countyCode) {
      return CodeToText[[countyCode]]
    }
  },
  data() {
    return {
      // visible: false,
      remarkProptVisible: false,
      remarkProptList: [],
      remarkProptIndex: -1,
      userName: '',
      userType: -1,
      //   showDialog: false,
      //   courseSection: '', // 课程金额
      //   bankName: '', // 银行名称
      //   bankId: '', // 银行Id
      //   orderId: '', // 订单Id
      //   type: 1, // 1:提课程款. 2:退佣金
      //   tid: '',
      //   courseTitle: '新增课程',
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 0,
        pageSize: 10
      },
      search: {
        orderNumbers: '',
        vipId: '',
        name: '',
        phone: '',
        startTime: '',
        endTime: ''
      },
      dialogFormVisible: false,
      course: {
        courseName: '',
        sign: '',
        introduce: '',
        address: '',
        imgUrl: '',
        courseType: '',
        teacher: '',
        courseTime: '',
        startTime: '',
        endTime: ''
      },
      dialogTableVisible: false,
      detailIndex: -1,
      permissionShow: false, // 权限数据未到达之前按钮隐藏
      // permissionList: [] // 权限数据列表
      addBtn: {}
    }
  },
  computed: {
    // 权限按钮
    setPermissionBtn() {
      return function(str, row) {
        let index = -1
        const buttonList = this.$store.getters.button
        index = buttonList.findIndex(item => item.value === str)
        // console.log(index)
        if (index !== -1) {
          row.btnName = buttonList[index].name
          return true
        } else {
          return false
        }
      }
    },
    splitName() {
      return function(name, status) {
        console.log('name:', name)
        console.log('status:', status)
        const nameArr = name.split('/')
        if (status === 0) {
          return nameArr[0]
        } else {
          return nameArr[1]
        }
      }
    }
  },
  watch: {
    'permissionList'(newVal) {
      this.permissionList = newVal
      this.permissionShow = true
    }
  },
  created() {
    this.getUserType()
    this.fetchData()
    // this.getPermissonFn()
  },
  mounted() {
    this.getPermissonFn() // 获取按钮权限 vuex的button
  },
  methods: {

    toaddPage() {
      this.$router.push({
        path: '/companyAccount/add',
        query: {}
      })
    },
    // 备注
    changeRemarks(uid, id, index) {
      this.remarkProptIndex = index
      console.log('--->', uid, id, index)
      const userData = ''
      this.remarkProptVisible = true
      this.$refs.remarke.request(uid, id)
    },
    remarkText() {
      this.fetchData()
    },
    getUserType() {
      switch (getLocalUserName()) {
        case 'admin':
          this.userType = 1
          break;
        case 'sale':
          this.userType = 2
          break;
        case 'casher':
          this.userType = 3
          break;
      }
    },
    // 查询
    // toSearch() {
    //   if (this.search.startTime) this.search.startTime = this.search.startTime + ' 00:00:00'
    //   if (this.search.endTime) this.search.endTime = this.search.endTime + ' 23:23:59'
    //   this.listLoading = true
    //   console.log(this.search)
    //   const newData = this.objectCon(this.listQuery, this.search)
    //   // console.log(this.search.startTime)
    //   // console.log(this.search.endTime)
    //   getCompanyAccountList(newData).then(response => {
    //     this.list = response.data.list
    //     this.total = response.data.total
    //     setTimeout(() => {
    //       this.listLoading = false
    //     }, 0.5 * 1000)
    //   })
    // },
    // 重置
    // toReset() {
    //   this.search.orderNumbers = ''
    //   this.search.vipId = ''
    //   this.search.name = ''
    //   this.search.phone = ''
    //   this.search.startTime = ''
    //   this.search.endTime = ''
    // },
    // isDialog(a) {
    //   this.visible = false
    //   this.showDialog = false
    // },
    fetchData() {
      this.listLoading = true
      const newData = this.objectCon(this.listQuery)
      console.log('newData:=>', newData)
      getCompanyAccountList(newData).then(response => {
        if (response.code === '200') {
          this.list = response.data.list
          console.log('订单数据：', this.list)
          this.total = response.data.total
          setTimeout(() => {
            this.listLoading = false
          }, 0.5 * 1000)
        } else {
          this.listLoading = false
        }
      })
    },
    see(index) {
      this.dialogTableVisible = true
      this.detailIndex = index
    },
    edit(row) {
      const query = { isEdit: true, data: row }
      this.$router.push({
        path: '/companyAccount/add',
        query
      })
    },
    del(id) {
      this.$confirm('是否删除此对公账号', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        const data = { id }
        delCompanyAccount(data).then(res => {
          if (res.code === '200') {
            Message.success('删除成功')
            this.fetchData()
          } else {
            Message.error(res.message + res.data)
          }
        })
      })
    },
    update(id, type) {
      const str = type === 1 ? '是否停用？' : '是否启用？'
      this.$confirm(str, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        type === 0 ? type = 1 : type = 0
        const data = { enableStatus: type, id }
        enableStatus(data).then(res => {
          if (res.code === '200') {
            Message.success('更新成功')
            this.fetchData()
          } else {
            Message.error(res.message + res.data)
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .inputBox{
    div{ margin-bottom: 10px;}
  }
</style>
